﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Management;
using System.IO;

namespace UsingStatement
{
    class Program
    {
        static void Main(string[] args)
        {
            GetMyDiskSpace2();
        }

        static void GetMyDiskSpace2()
        {
            DriveInfo theDriveInformation = new DriveInfo("c:");
            Console.WriteLine(theDriveInformation.AvailableFreeSpace);
        }

        static void GetMyDiskSpace()
        {
            Console.WriteLine(System.IO.Path.GetPathRoot(@"C:\Users\lex\Desktop\File.jpg"));
            string path = System.IO.Path.GetPathRoot(@"C:\Users\lex\Desktop\File.jpg");
            string param = "win32_logicaldisk.deviceid='j:'";
            path = path.Trim();
            param = "win32_logicaldisk.deviceid='"+path[0]+path[1]+"'";
            ManagementObject disk = new ManagementObject(param);
            disk.Get();
            string strSpace = disk["Size"].ToString();
            long iSpace = Int64.Parse(strSpace);
            Console.WriteLine("Logical Disk Size = " + iSpace + " bytes");
            Console.WriteLine("Logical Disk FreeSpace = " + disk["FreeSpace"] + "bytes");
        }

        static void TestImage()
        {
            Bitmap bmp2 = Image.FromFile(@"C:\Users\lex\Desktop\File.jpg") as Bitmap;
            Bitmap bmp = Image.FromFile(@"C:\Users\lex\Desktop\aaa.jpg") as Bitmap;

            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int bytes = bmpData.Stride * bmp.Height;
            byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
            Marshal.Copy(ptr, rgbValues, 0, bytes);

            // Scanning for non-zero bytes
            bool allBlack = true;
            for (int index = 0; index < rgbValues.Length; index++)
                if (rgbValues[index] != 0)
                {
                    allBlack = false;
                    break;
                }
            // Unlock the bits.
            bmp.UnlockBits(bmpData);
        }

        static void TestUsing()
        {
            C c = new C();
            using (c)
            {
                c.UseLimitedResource();
            }
            Console.WriteLine("Now outside using statement.");
            Console.ReadLine();
        }
    }

    class C : IDisposable
    {
        public void UseLimitedResource()
        {
            Console.WriteLine("Using limited resource...");
        }

        void IDisposable.Dispose()
        {
            Console.WriteLine("Disposing limited resource.");
        }
    }
}
